【今日湯底】
Take 2 strings s1 and s2 including only letters from a to z. Return a new sorted string, the longest possible, containing distinct letters - each taken only once - coming from s1 or s2.
Examples:
a = "xyaabbbccccdefww"
b = "xxxxyyyyabklmopq"
longest(a, b) -> "abcdefklmopqwxy"
a = "abcdefghijklmnopqrstuvwxyz"
longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"
(必須通過以下測試)
(ns longest.core-test
(:require [clojure.test :refer :all]
[longest.core :refer :all]))
(require '[clojure.string :as str])
(defn test-assert [act exp]
(is (= act exp)))
(deftest a-test1
(testing "longest"
(test-assert(longest "aretheyhere", "yestheyarehere"), "aehrsty")
(test-assert(longest "loopingisfunbutdangerous", "lessdangerousthancoding"), "abcdefghilnoprstu")
(test-assert(longest "inmanylanguages", "theresapairoffunctions"), "acefghilmnoprstuy")
))
【我的答案】
(ns longest.core)
(require '[clojure.string :as str])
(defn longest [s1 s2]
(str/join (sort (distinct (str/join s1 s2))))
)
【其他人的答案】
(ns longest.core)
(defn longest [& ss]
(->> ss (apply concat) (apply sorted-set) (apply str)))